import axios, { AxiosRequestConfig } from 'axios';
import { Message, Loading } from 'element-ui'
import cookies from "vue-cookies";

class OptionsModel {
    constructor(args) {
        this._set_assign(args);
        this._set_header(this.headers);
    }
    _set_assign(args) {
        args && Object.assign(this, args);
    }
    _set_header(headers) {
        headers['X-Requested-With'] = 'XMLHttpRequest';
        headers['Content-Type'] = 'application/json';
    };
    url = "";
    method = "";
    headers = {};
    timeout = 60000;
    data = {};
    params = {};
    query = ""
}
let isError = false;
var loadingInstance
class Axios {

    constructor(){

    }
    $alert(message, title, options) {
        if (!isError) {
            isError = true;
            window.bus.$alert(message, title, {
                callback: action => {
                    isError = false;
                    // routers.replace("/");
                }
            });
        }
    }
    codeAction(data) {
        let codeMapping = {
            401: () => this.$alert(data.msg, "系统提示"),
            500: () => this.$alert(data.msg, "系统提示"),
        };
        codeMapping[data.code] && codeMapping[data.code]();
        return Promise.resolve(data);
    }
    request(opts, isNew) {
        return new Promise((resolve) => {
            loadingInstance = Loading.service({
                lock: true,
                text: '数据加载中，请稍后...',
                spinner: 'el-icon-loading',
                background: 'rgba(0, 0, 0, 0.7)'
            })
            let options = isNew ? new OptionsModel(opts) : opts;
            // options.headers["Authorization"] = token;
            axios(options).then(res => {
                loadingInstance.close()
                this.codeAction(res.data).then((data) => resolve(data));
            }).catch(error => {
                loadingInstance.close()
                resolve({
                    msg: "无法加载响应数据:No data found for resource with given identifier",
                    code: "net::ERR_ADDRESS_UNREACHABLE",
                    data: error
                });
            });
        });
    }
    requestByNormal(options) {
        return axios(options)
            .then((res) => res)
            .catch((error) => error);
    }
    postJson(opts) {
        let options = new OptionsModel(opts);
        options.method = "post";
        return this.request(options, false);
    }

    post(opts) {
        let options = this.params(opts, 'data');
        options.method = "post";
        return this.request(options, false);
    }

    putJson(opts) {
        let options = new OptionsModel(opts);
        options.method = "put";
        return this.request(options, false);
    }

    put(opts) {
        let options = this.params(opts, 'data');
        return this.putJson(options);
    }

    get(opts) {
        let options = new OptionsModel(opts);
        options.method = "get";
        options.params = options.data;
        return this.request(options, false);
    }

    delete(opts) {
        let options = new OptionsModel(opts);
        options.method = "delete";
        options.params = options.data;
        return this.request(options, false);
    }
    params(opts, key = 'params') {
        let options = new OptionsModel(opts);
        let params = new URLSearchParams();
        console.log(params);
        for (let i in opts.data) {
            params.append(i, opts.data[i]);
        }
        options[key] = params;
        console.log(options);
        options.headers["Content-Type"] = "application/x-www-form-urlencoded";
        return options;
    }
}


export default Axios;
